<?php

namespace app\api\model;

use think\Model;

/**
 * Banner
 */
class OrderLog extends Model
{
    // 表名
    protected $name = 'order_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i:s',
    ];


   public function addLog($type,$order_id,$status,$msg,$reason=''){

       if(in_array($status,[6,7])){
           $logWhere[] = ['type','=',$type];
           $logWhere[] = ['order_id','=',$order_id];
           $logWhere[] = ['status','>',1];
           (new OrderLog())->where($logWhere)->delete();
       }else{
           $logWhere[] = ['type','=',$type];
           $logWhere[] = ['order_id','=',$order_id];
           $logWhere[] = ['status','>',$status];
           (new OrderLog())->where($logWhere)->delete();
       }


       $log = self::where(['type' => $type,'order_id' => $order_id,'status' => $status])->find();
       if($log){
           $data = [
               'type' => $type,
               'order_id' => $order_id,
               'status' => $status,
               'msg' => $msg,
               'reason' => $reason,
               'create_time' => time(),
           ];
           self::where(['id' => $log['id']])->update($data);
       }else{
           $model = new self();
           $data = [
               'type' => $type,
               'order_id' => $order_id,
               'status' => $status,
               'msg' => $msg,
               'reason' => $reason,
               'create_time' => time(),
           ];
           $model->insertGetId($data);
       }



       return true;
   }


   public function getStatusText($order_id,$type){
       return self::where(['order_id' => $order_id,'type' => $type ])->order('id asc')->group('status')->field('status,msg,reason,create_time')->select();
   }
}
